package com.xu.crawler.processor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xu.crawler.dao.HistoryRecordDao;
import com.xu.crawler.dao.HkStockDao;
import com.xu.crawler.dao.entity.HkStockDO;
import com.xu.crawler.enums.URLMapperEnum;
import com.xu.crawler.util.SpringUtils;
import org.assertj.core.util.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Json;

import javax.annotation.Resource;
import java.util.List;

/**
 * 港股股票列表
 * <p>
 * Created by clear on 2018/7/11.
 */
@Service
public class HkStockListPageProcessor implements PageProcessor {

    private Logger log = LoggerFactory.getLogger(StockHolderPageProcessor.class);
    private Site site = Site.me()
            .setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36")
            .setDomain(URLMapperEnum.TONGHUASHUN.toString())
            .addHeader("CooHokie", "Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1534648161; user=MDp4dWd1YW5nd3UyNDo6Tm9uZTo1MDA6NDI3NzczNDExOjcsMTExMTExMTExMTEsNDA7NDQsMTEsNDA7NiwxLDQwOzUsMSw0MDsxLDEsNDA7MiwxLDQwOzMsMSw0MDs1LDEsNDA7OCwwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMSw0MDoyNzo6OjQxNzc3MzQxMToxNTM0NjQ4MTYxOjo6MTUwNzU1Nzk2MDo2MDQ4MDA6MDoxNWY1Yzc5Y2ZlNWMxMDRmODY1ZjVmM2YxODk1YWE3ZGI6ZGVmYXVsdF8yOjA%3D; userid=417773411; u_name=xuguangwu24; escapename=xuguangwu24; ticket=4264daec408631d517c522817f8b59f9; cmsad_169_0=0; Hm_lvt_f79b64788a4e377c608617fba4c736e2=1534648297,1534648412; Hm_lvt_22a3c65fd214b0d5fd3a923be29458c7=1534648297,1534648412; historystock=HK0001%7C*%7CHK0700%7C*%7CHK0164; Hm_lpvt_22a3c65fd214b0d5fd3a923be29458c7=1534649164; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1=1534649164; Hm_lpvt_f79b64788a4e377c608617fba4c736e2=1534649164; v=Al09x66s2oT61r6oflibcQDnajJUepHMm671oB8imbTj1nKk58qhnCv-BXKs")
            .addHeader("Accept", "application/json, text/javascript, */*; q=0.01")
            .addHeader("Accept-Encoding", "gzip, deflate")
            .addHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
            .addHeader("Cache-Control", "no-cache")
            .addHeader("hexin-v", "Al09x66s2oT61r6oflibcQDnajJUepHMm671oB8imbTj1nKk58qhnCv-BXKs")
            .addHeader("ost", "stockpage.10jqka.com.cn")
            .addHeader("Referer", "http://stockpage.10jqka.com.cn/HK0001/holder/")
            .addHeader("X-Requested-Wit", "XMLHttpRequest")
            .setRetryTimes(3).setSleepTime(1000);

    @Resource
    private HkStockDao hkStockDao;

    @Override
    public void process(Page page) {
        try {
            Json json = page.getJson();
            String jsonp = json.toString();
            String res = jsonp.substring(jsonp.indexOf("(") + 1, jsonp.lastIndexOf(")"));
            JSONObject jsonObject = JSON.parseObject(res);
            JSONArray targetArray = JSON.parseArray(jsonObject.get("data").toString());
            List<HkStockDO> hkStockDOS = Lists.newArrayList();
            targetArray.forEach(temp -> {
                String[] values = temp.toString().split(",");
                HkStockDO targetDO = new HkStockDO();
                targetDO.setCode("HK" + values[1].substring(1));
                targetDO.setName(values[2]);
                targetDO.setNewestPrice(values[3]);
                targetDO.setRaiseLimitNum(values[4]);
                targetDO.setRaiseLimitPercent(values[5]);
                targetDO.setOpenPrice(values[11]);
                targetDO.setYesterdayPrice(values[12]);
                targetDO.setHighestPrice(values[13]);
                targetDO.setLowestPrice(values[14]);
                hkStockDOS.add(targetDO);
            });
            if (hkStockDao == null) {
                hkStockDao = SpringUtils.getBean(HkStockDao.class);
            }
            hkStockDao.batchSaveStocks(hkStockDOS);
        } catch (Throwable e) {
            log.error("process fail {}", e);
        }
    }

    @Override
    public Site getSite() {
        return site;
    }


}
